﻿// Copyright (c) 2010
// by http://openlightgroup.net/

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Collections.ObjectModel;
using Microsoft.VisualBasic;
using System.Windows;
using SilverlightDebate.SilverlightDebateWS;

namespace SilverlightDebate
{
    public class SilverlightXaps
    {
        #region GetXaps
        public static void GetXaps(EventHandler<GetXapsCompletedEventArgs> eh)
        {
            // Set up web service call
            WebServiceSoapClient WS = new WebServiceSoapClient();

            // Set the EndpointAddress
            WS.Endpoint.Address = new EndpointAddress(GetBaseAddress());

            RIAAuthenticationHeader RIAAH = (RIAAuthenticationHeader)Application.Current.Resources["RIAAuthenticationHeader"];

            WS.GetXapsCompleted += eh;
            WS.GetXapsAsync(RIAAH);
        }
        #endregion

        #region GetBaseAddress
        private static string GetBaseAddress()
        {
            // Get the web address of the .xap that launched this application
            string strBaseWebAddress = App.Current.Host.Source.AbsoluteUri;
            // Find the position of the ClientBin directory
            int PositionOfClientBin =
                App.Current.Host.Source.AbsoluteUri.ToLower().IndexOf(@"/clientbin");
            // Strip off everything after the ClientBin directory
            strBaseWebAddress = Strings.Left(strBaseWebAddress, PositionOfClientBin);
            // Return the base address
            return string.Format(@"{0}/{1}", strBaseWebAddress, "WebService.asmx");
        }
        #endregion
    }
}